package clinsistem.a.IU;


import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;


import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import clinsistem.b.negocio.controladores.Fachada;
import clinsistem.b.negocio.controladores.GenericException;
import javax.swing.JSeparator;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;


public class TelaAlterarSenha extends JInternalFrame  implements InternalFrameListener {
	private static final long serialVersionUID = 1L;
	private JPasswordField campoSenhaAtual;
	private JPasswordField campoNovaConfirma;
	private JPasswordField campoNovaSenha;
	


	public TelaAlterarSenha() {
		setFrameIcon(new ImageIcon(TelaAlterarSenha.class.getResource("/imagens/Login/chave-icon.png")));
		setClosable(true);
		setIconifiable(true);
		setTitle("Alterar Senha");
		setBounds(10, 10, 357, 358);
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel painelPrincipal = new JPanel();
		getContentPane().add(painelPrincipal, BorderLayout.CENTER);
		painelPrincipal.setLayout(null);
		
		JPanel painelSenha = new JPanel();
		painelSenha.setBounds(10, 58, 199, 254);
		painelPrincipal.add(painelSenha);
		painelSenha.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		painelSenha.setLayout(null);
		
		JLabel senhaAtual = new JLabel("Senha atual:");
		senhaAtual.setFont(new Font("Tahoma", Font.PLAIN, 14));
		senhaAtual.setBounds(10, 11, 84, 23);
		painelSenha.add(senhaAtual);
		
		campoSenhaAtual = new JPasswordField();
		campoSenhaAtual.setBounds(10, 45, 176, 25);
		painelSenha.add(campoSenhaAtual);
		campoSenhaAtual.setColumns(10);
		
		JLabel novaSenha = new JLabel("Nova senha:");
		novaSenha.setFont(new Font("Tahoma", Font.PLAIN, 14));
		novaSenha.setBounds(10, 81, 84, 34);
		painelSenha.add(novaSenha);
		
		JLabel novaSenhaConfirmacao = new JLabel("Nova senha (confirma\u00E7\u00E3o):");
		novaSenhaConfirmacao.setFont(new Font("Tahoma", Font.PLAIN, 14));
		novaSenhaConfirmacao.setBounds(10, 162, 176, 34);
		painelSenha.add(novaSenhaConfirmacao);
		
		campoNovaConfirma = new JPasswordField();
		campoNovaConfirma.setBounds(10, 207, 176, 25);
		painelSenha.add(campoNovaConfirma);
		campoNovaConfirma.setColumns(10);
		
		campoNovaSenha = new JPasswordField();
		campoNovaSenha.setBounds(10, 126, 176, 25);
		painelSenha.add(campoNovaSenha);
		campoNovaSenha.setColumns(10);
		
		JLabel alterarSenha = new JLabel("Alterar Senha");
		alterarSenha.setBounds(10, 14, 113, 20);
		painelPrincipal.add(alterarSenha);
		alterarSenha.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 45, 347, 2);
		painelPrincipal.add(separator);
		
		JButton botaoConfirmar = new JButton("Confirmar");
		botaoConfirmar.setBounds(219, 267, 120, 45);
		painelPrincipal.add(botaoConfirmar);
		botaoConfirmar.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel chave = new JLabel("");
		chave.setIcon(new ImageIcon(TelaAlterarSenha.class.getResource("/imagens/Login/chave-icon.png")));
		chave.setBounds(219, 79, 120, 177);
		painelPrincipal.add(chave);
		botaoConfirmar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				alterar();
			}
			
		});
		
		this.addInternalFrameListener(this);
		
		this.getActionMap().put("exit", new AbstractAction(){
			 
            public void actionPerformed(ActionEvent e) {
                dispose();
 
            }
        });
        InputMap map = this.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        map.put(stroke,"exit");
	}
	
	
	public void alterar() {
		String senha = "";
		String novaSenha = "";
		String novaSenha2 = "";
		char v[] = campoSenhaAtual.getPassword();
		for (int i = 0; i < v.length; i++) { 
			senha+=v[i];
		}
		
		char v2[] = campoNovaSenha.getPassword();
		for (int i = 0; i < v2.length; i++) {
			novaSenha+=v2[i];
		}
		
		char v3[] = campoNovaConfirma.getPassword();
		for (int i = 0; i < v3.length; i++) {
			novaSenha2+=v3[i];
		}
		
		Fachada fachada = new Fachada();
		try {
			fachada.alterarSenha(senha, novaSenha, novaSenha2, TelaPrincipal.logado);
			JOptionPane.showMessageDialog(null, "Senha alterada com sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
			foco();
			this.dispose();
		} catch (GenericException e1) { 
			JOptionPane.showMessageDialog(null, e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void foco() {
		this.campoSenhaAtual.requestFocus();
	}

	@Override
	public void internalFrameActivated(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void internalFrameClosed(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		TelaPrincipal.alterarIsOpen=false;
	}


	@Override
	public void internalFrameClosing(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void internalFrameDeactivated(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void internalFrameDeiconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void internalFrameIconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void internalFrameOpened(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	
}

